﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace FurnitureRentalSystem
{
    public partial class MainForm : Form
    {
        private MySql.Data.MySqlClient.MySqlConnection conn;
        private string connString;

        public MainForm()
        {
            InitializeComponent();
            splitContainer1.Visible = false;
            registerCustomerPanel.Visible = false;
            try
            {
                //Retrieve connection string from App.config file 
                connString = System.Configuration.ConfigurationManager.AppSettings[0];
                conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
            }
            catch (Exception)
            {
                MessageBox.Show("Error reading database connection string", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //Checks user input on the Register Customer page
        private bool registerCustomerValidation()
        {
            List<TextBox> controls = new List<TextBox>();
            controls.Add(firstNameTxt);
            controls.Add(LastNameTxt);
            controls.Add(PhoneNumTxt);
            controls.Add(AddressTxt);
            controls.Add(SSNTxt);

            foreach (TextBox next in controls)
            {
                if (next.Text == null)
                {
                    MessageBox.Show("Please check the value of " + next.ToString() + " and try again");
                    return false;
                }
            }

            try
            {
                long tempValue1 = long.Parse(SSNTxt.Text);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Please enter a valid number for SSN");
                return false;
            }

            try
            {
                long tempValue = long.Parse(PhoneNumTxt.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please enter a valid number for phone number");
                return false;
            }

            if (SSNTxt.Text.Length != 9)
            {
                MessageBox.Show("SSN must be equal to 9 digits");
                return false;
            }

            if (PhoneNumTxt.Text.Length != 10)
            {
                MessageBox.Show("Phone number must be equal to 10 digits");
                return false;
            }
            return true;
        }


        //Enables menu items baesd on a successful login and admin/non-admin credentials
        private void enableMenuItems(bool admin)
        {
            //Temporary
            customerToolStripMenuItem.Enabled = false;
            //End Temporary
            if (admin == false)
            {
                searchToolStripMenuItem.Enabled = true;
            }
            else
            {
                searchToolStripMenuItem.Enabled = true;
                adminToolStripMenuItem.Enabled = true;
            }
        }

        //Enables panel items baesd on a successful login and admin/non-admin credentials
        private void enableMainPanel(bool admin)
        {
            if (admin == false)
            {
                splitContainer1.Visible = true;
            }
            else
            {
                splitContainer1.Visible = true;
                button1.Enabled = true;
            }
        }

        //Handles login validation for an employee
        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoginDlg loginForm = new LoginDlg(connString);
            if (loginForm.ShowDialog() == DialogResult.OK)
            {
                enableMenuItems(false);
                enableMainPanel(false);
            }
        }

        //Handles login validation for an administrator
        private void adminLoginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AdminLoginDlg adminLoginForm = new AdminLoginDlg(this);
            if (adminLoginForm.ShowDialog() == DialogResult.OK)
            {
                enableMenuItems(true);
                enableMainPanel(true);
            }
        }


        //Connects to the database and searches for rows based on user input
        private void button7_Click(object sender, EventArgs e)
        {
            MySql.Data.MySqlClient.MySqlDataReader reader = null;
            try
            {
                //Connect to database and run a query based on user input
                conn.Open();
                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
                cmd.CommandText = "SELECT * FROM GroupWHL.Furniture WHERE Type='" + fCatTxt.Text + "' AND Color='" + colorTxt.Text + "'";
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                reader = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (reader.HasRows)
            {
                //If the reader has rows print results to screen 
                resultsPanel.Visible = true;
                resultsPanel.BringToFront();
                searchFurniturePanel.SendToBack();
                resultsTextBox.Clear();
                resultsTextBox.Text += "ID" + "\t" + "Type" + "\t" + "Style" + "\t" + "Color" + "\t" + "Quantity" + "\t" + "Material" + "\t" + "Description";
                while (reader.Read())
                {
                    String id = reader.GetString(reader.GetOrdinal("iID"));
                    String type = reader.GetString(reader.GetOrdinal("Type"));
                    String style = reader.GetString(reader.GetOrdinal("Furniture Style"));
                    String color = reader.GetString(reader.GetOrdinal("Color"));
                    String quantity = reader.GetString(reader.GetOrdinal("Quantity"));
                    String material = reader.GetString(reader.GetOrdinal("Material"));
                    String description = reader.GetString(reader.GetOrdinal("Description"));
                    resultsTextBox.Text += Environment.NewLine;
                    resultsTextBox.Text += id + "\t" + type + "\t" + style + "\t" + color + "\t" + quantity + "\t" + material + "\t" + description;
                }
            }
            else
            {
                MessageBox.Show("No results found");
            }
            conn.Close();
        }

        //Shows the Search Furniture panel
        private void searchFurBtn_Click(object sender, EventArgs e)
        {
            searchFurBtn.Enabled = false;
            regCusBtn.Enabled = true;
            searchFurniturePanel.Visible = true;
            searchFurniturePanel.BringToFront();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            resultsPanel.Visible = false;
            searchFurniturePanel.Visible = true;
            searchFurniturePanel.BringToFront();
        }
        
        //Shows the Register Customer panel 
        private void regCusBtn_Click(object sender, EventArgs e)
        {
            regCusBtn.Enabled = false;
            searchFurBtn.Enabled = true;
            registerCustomerPanel.Visible = true;
            registerCustomerPanel.BringToFront();
        }

        //Register a new customer 
        private void registerCusBtn_Click(object sender, EventArgs e)
        {
            if (registerCustomerValidation() == true)
            {
                try
                {
                    //Opens a new connection and attempts to insert a new customer based on user input
                    MySql.Data.MySqlClient.MySqlDataReader reader = null;
                    conn.Open();
                    MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
                    cmd.CommandText = "INSERT INTO `GroupWHL`.`customer` (`First Name`, `Last Name`, `Address`, `Telephone#`, `SSN`) VALUES ('" + firstNameTxt.Text + "', '" + LastNameTxt.Text + "', '" + AddressTxt.Text + "', '" + PhoneNumTxt.Text + "', '" + SSNTxt.Text + "')";
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    reader = cmd.ExecuteReader();
                    reader.Close();

                    //Displays the assigned Customer ID to the employee
                    cmd.CommandText = "SELECT `cID` from `GroupWHL`.`customer` WHERE SSN = " + SSNTxt.Text + " AND `Telephone#` = " + PhoneNumTxt.Text;
                    reader = cmd.ExecuteReader();
                    String cID = "";
                    while (reader.Read())
                    {
                        cID = reader.GetString(reader.GetOrdinal("cID"));
                    }
                    MessageBox.Show("Registration Successful" + Environment.NewLine + firstNameTxt.Text + " " + LastNameTxt.Text + " has been assigned ID# " + cID);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
